package org.wanxian.stores.db;

import java.util.ArrayList;

import org.wanxian.stores.model.User;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class UserDao {
	private DatabaseOpenHelper dbHelper;
	private static final String TABLE_NAME = "user";
	
	public UserDao(Context context) {
		dbHelper = new DatabaseOpenHelper(context);
	}
	
	public boolean isExist(User user) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, "id = ?", new String[] {String.valueOf(user.getId())}, null, null, null);
		if(cursor.getCount() > 0) {
			cursor.close();
			db.close();
			return true;
		}
		return false;
	}
	
	public int insert(User user) {
		User bef = queryById(user.getId());
		if(bef != null) {
			deleteById(bef.getId());
		}
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("name", user.getName());
		values.put("phone", user.getPhone());
		values.put("sex", user.getSex());
		values.put("password", user.getPassword());
		values.put("image", user.getImage());
		int id = (int) db.insert(TABLE_NAME, null, values);
		return id;
	}
	
	public int updatePassWordById(String password, String id) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues value = new ContentValues();
		value.put("password", password);
		int index = (int) db.update(TABLE_NAME, value, "id=?", new String[]{id});
		db.close();
		return index;
	}
	
	public int update(User user) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues value = new ContentValues();
		value.put("password", user.getPassword());
		value.put("phone", user.getPhone());
		value.put("name", user.getName());
		value.put("sex", user.getSex());
		value.put("image", user.getImage());
		int index = (int) db.update(TABLE_NAME, value, "id = ?", new String[]{String.valueOf(user.getId())});
		db.close();
		return index;
	}
	
	public void deleteById(int id) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.delete(TABLE_NAME, "id = ?", new String[] {String.valueOf(id)});
		db.close();
	}
	public User queryById(int id) {
		User user = null;
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, "id = ?", new String[]{String.valueOf(id)}, null, null, null);
		while(cursor.moveToNext()) {
			String name = cursor.getString(cursor.getColumnIndex("name"));
			String phone = cursor.getString(cursor.getColumnIndex("phone"));
			String password = cursor.getString(cursor.getColumnIndex("password"));
			String image = cursor.getString(cursor.getColumnIndex("image"));
			String sex = cursor.getString(cursor.getColumnIndex("sex"));
			String address = cursor.getString(cursor.getColumnIndex("address"));
			user = new User(id, name, phone, sex,
					password, image, address);
		}
		cursor.close();
		db.close();
		return user;
	}
	
	public User queryByName(String name) {
		User user = null;
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, "name=?", new String[]{name}, null, null, null);
		while(cursor.moveToNext()) {
			int id = cursor.getInt(cursor.getColumnIndex("id"));
			String phone = cursor.getString(cursor.getColumnIndex("phone"));
			String password = cursor.getString(cursor.getColumnIndex("password"));
			String image = cursor.getString(cursor.getColumnIndex("image"));
			String sex = cursor.getString(cursor.getColumnIndex("sex"));
			String address = cursor.getString(cursor.getColumnIndex("address"));
			user = new User(id, name, phone, sex,
					password, image, address);
		}
		cursor.close();
		db.close();
		return user;
	}
	
	public ArrayList<User> queryAll() {
		ArrayList<User> list = new ArrayList<User>();
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
		while(cursor.moveToNext()) {
			int id = cursor.getInt(cursor.getColumnIndex("id"));
			String name = cursor.getString(cursor.getColumnIndex("name"));
			String phone = cursor.getString(cursor.getColumnIndex("phone"));
			String password = cursor.getString(cursor.getColumnIndex("password"));
			String image = cursor.getString(cursor.getColumnIndex("image"));
			String sex = cursor.getString(cursor.getColumnIndex("sex"));
			String address = cursor.getString(cursor.getColumnIndex("address"));
			User user = new User(id, name, phone, sex,
					password, image, address);
			list.add(user);
		}
		cursor.close();
		db.close();
		return list;
	}
	
	public int getCount() {
		return queryAll().size();
	}
}







